<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<style>
    .parsley-errors-list{
        display: none;
    }
</style>
<script>
    /*选择价表自动补全*/
    function autoCompleteDrugs(inputId,idex){
        var storageId = $("#applyTableId .table-selected").find("td").eq(7).text();;
        var url=ctx+"/doctor/prescriptionItem/autoComplete?drugClass=B&storageId="+storageId;
        autoComplete(inputId,url,
            function(data){
                var rows = [];
                for(var i=0; i<data.length; i++){
                    rows[rows.length] = {
                        data:data[i],
                        value:data[i].priceId,
                        result:data[i].drugName
                    };
                }
                return rows;
            },function(data, i, max){
                return data.inputCode+"|"+data.drugName+"|"+data.packageUnit+"|"+data.price+"元 |"+data.firmName+"|库存量："+data.quantity;
            },function(event,data,formatted){
                /*判断选中药品是否已添加，如果添加，则不让再次添加*/
                var bl = true;
                $("#herbal_ul li div span input").each(function(){
                    if(($(this).val()==data.drugCode)&&($(this).attr('id')!='drugCode'+idex)){
                        bl = false;
                        return false;
                    }
                });
                if(bl){
                    var code=$('#orderCode'+idex).val();
                    if(code==null || code ==""){
                        addDrugList('herbalLength');
                    }
                    $('#orderCode'+idex).attr('value',data.drugCode);
                    $('#singleDose'+idex).attr('value',data.dosePerUnit);
                    $('#singleDoseUnit'+idex).attr('value',data.minUnits);
                    $('#firmId'+idex).attr('value',data.firmId);
                    $('#firmName'+idex).attr('value',data.firmName);
                    $('#price'+idex).attr('value',data.price);
                    $('#packageSpec'+idex).attr('value',data.packageSpec);
                    $('#sumPrice'+idex).attr('value',data.price);
                    $('#drugUnits'+idex).attr('value',data.packUnit);
                    $('#spec'+idex).attr('value',data.minSpec);
                    $('#unit'+idex).attr('value',data.minUnits);
                    $('#inputCode'+idex).attr('value',data.inputCode);
                    $('#priceId'+idex).attr('value',data.priceId);
                    $('#gram'+idex).html(data.packageUnit);

                }else{
                    $('#drugNameIds'+idex).val('');
                    toastr.info("该药品已添加，不能重复添加！");
                    return false;
                }

            });
    }


    function addDrugList(id){

            rowNum =$("#"+id).val();
            var html= "";
            html+='<li id="c_li_'+rowNum+'">' +
                '<div>' +
                '<span class="col-sm-6">' +
                '<input name="treatItemList['+rowNum+'].orderText" class="form-control" data-parsley-required="true" placeholder="选择药品" id="drugNameIds'+rowNum+'" value=""/> '+
                '</span>'+
                '<span class="col-sm-4">' +
                '<input type="hidden" name="treatItemList['+rowNum+'].spec" id="spec'+rowNum+'" value=""/>' +
                '<input type="hidden" name="treatItemList['+rowNum+'].inputCode" id="inputCode'+rowNum+'" value=""/>'+
                '<input type="hidden" name="treatItemList['+rowNum+'].orderCode" id="orderCode'+rowNum+'" value=""/>'+
                '<input type="hidden" name="treatItemList['+rowNum+'].firmId" id="firmId'+rowNum+'" value=""/>'+
                '<input type="hidden" name="treatItemList['+rowNum+'].firmName" id="firmName'+rowNum+'" value=""/>'+
                '<input type="hidden" name="treatItemList['+rowNum+'].singleDose" id="singleDose'+rowNum+'" value=""/>'+
                '<input type="hidden" name="treatItemList['+rowNum+'].singleDoseUnit" id="singleDoseUnit'+rowNum+'" value=""/>'+
                '<input type="hidden" name="treatItemList['+rowNum+'].price" id="price'+rowNum+'" value=""/>'+
                '<input type="hidden" name="treatItemList['+rowNum+'].drugUnits" id="drugUnits'+rowNum+'" value=""/>'+
                '<input type="hidden" name="treatItemList['+rowNum+'].packageSpec" id="packageSpec'+rowNum+'" value=""/>'+
                '<input type="hidden" name="treatItemList['+rowNum+'].abidance" id="abidance'+rowNum+'" value=""/>'+
                '<input type="hidden" name="treatItemList['+rowNum+'].sumPrice" id="sumPrice'+rowNum+'" value=""/>'+
                '<input type="hidden" name="treatItemList['+rowNum+'].unit" id="unit'+rowNum+'" value=""/>'+
                '<input type="hidden" name="treatItemList['+rowNum+'].priceId" id="priceId'+rowNum+'" value=""/>'+
                '<input type="text" name="treatItemList['+rowNum+'].amount" id="amount'+rowNum+'" data-parsley-required="true" class="form-control"  placeholder="数量" value="1" />'+
                '</span>'+
                '<span class="col-sm-1 gram" id="gram'+rowNum+'"></span> <i class="col-sm-1 fa fa-close" style="float:right;" onclick="delDrug('+rowNum+')"></i>'+
                '<span class="col-sm-12 mt10"><input placeholder="备注" name="treatItemList['+rowNum+'].remarks" class="col-sm-12" style="height: 30px;" type="text"></span>'+
                '</div>'+
                '</li>';
            $("#herbal_ul").append(html);
            autoCompleteDrugs('drugNameIds'+rowNum+'',rowNum);
            rowNum++;
            $("#"+id).val(rowNum);
            html="";

    }

    function delDrug(idx){
        $("#c_li_"+idx).remove();
    }
</script>
<div class="panel-body">
    <div class="btn-group pull-left">
        <button type="button" class="btn btn-success" onclick="addDrugList('herbalLength')">添加药品</button>
        <button type="button" class="btn btn-success" onclick="saveChinses('${doctorTempla.id}')">保存</button>
    </div>
    <form id="prescChineseItemForm" data-parsley-validate="" onsubmit="return formSaveLoad('prescChineseItemDiv','inputItemForm','${ctx}/doctor/prescriptionItem/save?inoroutFlag=0','${ctx}/doctor/prescriptionItem/chineseIndex?applyId=${prescriptionItem.applyId}');" method="post" class="form-horizontal">
        <input type="hidden" name="orderClass" value="B">
        <div class="col-lg-10 pull-right">
            <%--<label class="col-sm-2 control-label">是否代煎：</label>--%>
            <%--<div class="col-sm-2">--%>
                <%--<select name="isDecoction" id="isDecoction" class="form-control" data-parsley-required="true">--%>
                    <%--<c:forEach items="${clinicDict}" var="dict">--%>
                        <%--<option value="${dict.id}">${dict.label}</option>--%>
                    <%--</c:forEach>--%>
                    <%--<option value="1">不代煎</option>--%>
                <%--</select>--%>
            <%--</div>--%>
            <label class="col-sm-1 control-label">途径：</label>
            <div class="col-sm-2">
                <select name="administration" id="administration" class="form-control" data-parsley-required="true">
                    <c:forEach items="${administrationDicts}" var="dict">
                        <option value="${dict.id}">${dict.label}</option>
                    </c:forEach>
                </select>
            </div>

            <label class="col-sm-1 control-label">频次：</label>
            <div class="col-sm-2">
                <select name="performFreq" id="performFreq" class="form-control" data-parsley-required="true">
                    <c:forEach items="${frequency}" var="dict">
                        <option value="${dict.id}">${dict.freqDesc}</option>
                    </c:forEach>
                </select>
            </div>
            <label class="col-sm-1 control-label">剂数：</label>
            <div class="col-sm-1"><input name="repetition" value="7" class="form-control" ></div>
        </div>
        <div class="clearfix"></div>
        <ul id="herbal_ul" class="drugCenterUl">
            <input type="hidden" id="herbalLength" value="${chinaList.size()}">
            <c:if test="${chinaList.size()>0}">
                <c:forEach items="${chinaList}" var="item" varStatus="idx">

                    <li id="c_li_${item.id}">
                        <div>
                            <span class="col-sm-6">
                                   <input name="treatProjectItemList[${idx.index}].orderText" data-parsley-required="true" class="form-control"  placeholder="选择药品" id="drugNameIds${idx.index}" value="${item.orderText}" />
                           </span>
                            <span class="col-sm-4">
                                <input type="hidden" name="treatItemList[${idx.index}].spec" id="spec${idx.index}" value="${item.spec}"/>
                                <input type="hidden" name="treatItemList[${idx.index}].id" id="spec${idx.index}" value="${item.id}"/>
                                <input type="hidden" name="treatItemList[${idx.index}].inputCode" id="inputCode${idx.index}" value="${item.inputCode}"/>
                                <input type="hidden" name="treatItemList[${idx.index}].orderCode" id="orderCode${idx.index}" value="${item.orderCode}"/>
                                 <input type="hidden" name="treatItemList[${idx.index}].firmId" id="firmId${idx.index}" value="${item.firmId}"/>
                                <input type="hidden" name="treatItemList[${idx.index}].firmName" id="firmName${idx.index}" value="${item.firmName}"/>
                                <input type="hidden" name="treatItemList[${idx.index}].singleDose" id="singleDose${idx.index}" value="${item.singleDose}"/>
                                <input type="hidden" name="treatItemList[${idx.index}].singleDoseUnit" id="singleDoseUnit${idx.index}" value="${item.singleDoseUnit}"/>
                                <input type="hidden" name="treatItemList[${idx.index}].price" id="price${idx.index}" value="${item.price}"/>
                                <input type="hidden" name="treatItemList[${idx.index}].drugUnits" id="drugUnits${idx.index}" value="${item.drugUnits}"/>
                                <input type="hidden" name="treatItemList[${idx.index}].packageSpec" id="packageSpec${idx.index}" value="${item.packageSpec}"/>
                                <input type="hidden" name="treatItemList[${idx.index}].abidance" id="abidance${idx.index}" value="${item.abidance}"/>
                                <input type="hidden" name="treatItemList[${idx.index}].sumPrice" id="sumPrice${idx.index}" value="${item.sumPrice}"/>
                               <%--<input type="hidden" name="treatItemList[${idx.index}].unit" id="unit${idx.index}" value="${item.unit}"/>--%>
                                <input type="text" name="treatItemList[${idx.index}].amount" id="amount${idx.index}" class="form-control"  placeholder="数量" value="${item.amount}"/>
                            </span>
                            <span class="col-sm-1 gram">${fns:getDictLabel(item.drugUnits,'SPEC_UNIT' ,'')}</span>
                            <span class="col-sm-12 mt10"><input placeholder="备注" name="treatItemList[${idx.index}].remarks" class="col-sm-12 form-control"  value="${item.remarks}"  style="height: 30px;" type="text"></span>
                            <%--<i class="col-sm-1 fa fa-close" style="float:right;" onclick="delDrug('${item.id}')"></i>--%>
                        </div>

                    </li>
                </c:forEach>
            </c:if>
        </ul>
    </form>

</div>




